import { createParamDecorator, ExecutionContext } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';

export const CurrentUser = createParamDecorator(
  (data: unknown, ctx: ExecutionContext) => {
    const request = ctx.switchToHttp().getRequest();
    // 添加调试日志
    const token = request.headers.authorization?.replace('Bearer ', '');
    if (!token) {
      return null;
    }
    const jwtService = new JwtService({
      secret: process.env.JWT_SECRET,
    });
    
    try {
      const decoded = jwtService.decode(token);
      // 添加调试日志
      // console.log('token解码:', decoded);
      return decoded;
    } catch (error) {
      console.error('token解码错误:', error);
      return null;
    }
  },
);